package everyday.nowcoder;
import java.util.*;
/**
 * @Author: wenjingyuan
 * @Date: 2022/11/22/21:30
 * @Description:NC48 在旋转过的有序数组中寻找目标值
 */
public class NC48 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param nums int整型一维数组
     * @param target int整型
     * @return int整型
     */
    public int search (int[] nums, int target) {
        if(nums==null || nums.length==0||target<0) return -1;
        int low=0;
        int high= nums.length-1;
        int mid=0;
        while(low<=high){
            mid=low+(high-low)/2;
            if(target==nums[mid]){
                return  mid;
            }else if(nums[low]<nums[mid]){
                if(target>nums[mid] || target<nums[low]){
                    low=mid+1;
                }else{
                    high=mid-1;
                }
            }else{
                if(target<nums[mid]||target>nums[high]){
                    high=mid-1;
                }else{
                    low=mid+1;
                }
            }
        }
        return -1;
    }
}
